/*-----------------------------------------------------------------------------
    
    Copyright (C) 2005 T2-Design Corp. All rights reserved.
    
    File: obj_bitmap.c
    
    Content: 
    
    History: 
    2005/11/16 by cwh
        Inherited from M3328C.
-----------------------------------------------------------------------------*/
#include <sys_config.h>

#include <api/libc/string.h>
#include <api/libosd/osd_lib.h>
#include <api/libosd/osd_common_draw.h>
#include "osd_lib_internal.h"
//---------------------------- PRIVATE VARIABLES ----------------------------//


//---------------------------- PRIVATE FUNCTIONS ----------------------------//


//---------------------------- PUBLIC FUNCTIONS -----------------------------//

void OSD_DrawBitmapCell(PBITMAP pCtrl,UINT8 bStyleIdx,UINT32 nCmdDraw)
{
	lpVSCR  	pVscr;
	struct OSDRect	*objframe;	
	ID_RSC		RscId;
	PWINSTYLE  	lpWinSty;
	UINT32		fgColor;
	OBJECTINFO	RscLibInfo;
	UINT8 		bAlign,hAligin,vAlign;
	UINT16 		x,y,w,h;

	objframe	= &pCtrl->head.frame;	
	lpWinSty 	= g_osd_rsc_info.osd_get_win_style(bStyleIdx);
	fgColor		= lpWinSty->wFgIdx;
	RscId 		= LIB_ICON;
	bAlign 		= pCtrl->bAlign;

	pVscr = OSD_DrawObjectFrame(objframe,bStyleIdx);

	// for 16bit color mode. use bg color to draw the trans color, not use fg color.
	if(OSD_ColorModeIs16bit(pVscr->bColorMode)) 
		fgColor = lpWinSty->wBgIdx;

    if(OSD_ColorModeIs32bit(pVscr->bColorMode))    //20131118 luke add for 32 bit UI
    {
        fgColor = lpWinSty->wBgIdx;
    }

	if(pCtrl->wIconID)
	{

		g_osd_rsc_info.osd_get_obj_info(RscId, pCtrl->wIconID, &RscLibInfo);
		w = RscLibInfo.m_objAttr.m_wActualWidth;
		h = RscLibInfo.m_objAttr.m_wHeight;
		hAligin = GET_HALIGN(bAlign);
		vAlign  = GET_VALIGN(bAlign);
		if(hAligin==C_ALIGN_LEFT)
			x = objframe->uLeft + pCtrl->bX;
		else if(hAligin==C_ALIGN_RIGHT)
			x = objframe->uLeft + objframe->uWidth - pCtrl->bX - w;
		else
		{
			if(w < (objframe->uWidth - (pCtrl->bX<<1) ) )
				x = objframe->uLeft + ((objframe->uWidth - w)>>1);
			else
				x = objframe->uLeft + pCtrl->bX;				
		}
		
		if(vAlign == C_ALIGN_TOP)
			y = objframe->uTop + pCtrl->bY;
		else if(vAlign == C_ALIGN_BOTTOM)
			y = objframe->uTop + objframe->uHeight - pCtrl->bY - h;
		else
		{
			if( h < (objframe->uHeight - (pCtrl->bY<<1) ) )
				y = objframe->uTop + ((objframe->uHeight - h)>>1);
			else
				y = objframe->uTop + pCtrl->bY;				
		}		
		
		g_osd_rsc_info.osd_get_obj_info(RscId,pCtrl->wIconID,&RscLibInfo);
			
#ifdef BIDIRECTIONAL_OSD_STYLE
		if (RscLibInfo.m_objAttr.m_mirrorFlag == TRUE)
			OSD_DrawPictureInverse(x,y,pCtrl->wIconID,LIB_ICON,fgColor,pVscr); // inverse twice -> make it postive again!
		else
#endif
		OSD_DrawPicture(x,y,pCtrl->wIconID,LIB_ICON,fgColor,pVscr);
	}
}

 
VACTION OSD_BitmapKeyMap(POBJECT_HEAD pObj, UINT32 vkey)
{
 	VACTION Action = VACT_PASS;

	switch(vkey)
	{
		case V_KEY_ENTER:
			Action = VACT_ENTER;
			break;
		default:
			break;
	}

	return Action;
}


PRESULT OSD_BitmapProc(POBJECT_HEAD pObj, UINT32 msg_type, UINT32 msg,UINT32 param1)
{
	PRESULT Result;

	if(msg_type == MSG_TYPE_KEY)
	{
		VACTION Action;
		UINT32 vkey;
		BOOL bContinue;    	

		Result = OSD_ObjCommonProc(pObj,msg,param1,&vkey,&Action,&bContinue);
		if(!bContinue)
			goto CHECK_LEAVE;

		switch(Action)
		{
			case VACT_CLOSE:
CLOSE_OBJECT:    		
				Result = OSD_ObjClose(pObj,C_CLOSE_CLRBACK_FLG);
				goto EXIT;
				break;
			default:
				Result = OSD_SIGNAL(pObj, EVN_UNKNOWN_ACTION, (Action<<16) | vkey, param1);
				break;
		}
	}
	else// if(msg_type==MSG_TYPE_EVNT)
	{
		Result = OSD_SIGNAL(pObj,msg_type, msg, param1);
	}

CHECK_LEAVE:
	if(!(EVN_PRE_OPEN == msg_type))
		CHECK_LEAVE_RETURN(Result,pObj);

EXIT:
	return Result;		
}
